package com.achilles.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.achilles.activity.MainActivity;
import com.achilles.bean.DataBean;
import com.achilles.bean.GiftBean;
import com.achilles.callback.IListFragmentChanged;
import com.achilles.callback.IProjectOrGiftDialogItemClick;
import com.achilles.database.DBManager;
import com.achilles.playtask.R;
import com.achilles.utils.DateUtils;
import com.achilles.widget.ProjectOrGiftItemDialog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftFragment extends BaseListFragment {

    private ProjectOrGiftItemDialog mProjectOrGiftItemDialog;
    private List<DataBean> mGiftBeanList;
    private IListFragmentChanged IListFragmentChanged;

    public static GiftFragment getInstance(IListFragmentChanged IListFragmentChanged) {
        GiftFragment giftFragment = new GiftFragment();
        giftFragment.setIListFragmentChanged(IListFragmentChanged);
        return giftFragment;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_gift;
    }

    @Override
    protected void onViewCreated(View view) {
        super.onViewCreated(view);
        mProjectOrGiftItemDialog = new ProjectOrGiftItemDialog(getActivity());
        mProjectOrGiftItemDialog.setIProjectOrGiftDialogItemClick(new DialogDialogItemClickOr());
        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GiftBean giftBean = (GiftBean) mGiftBeanList.get(position);
                showGiftDialog(giftBean);
            }
        });
        getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long
                    id) {
                mProjectOrGiftItemDialog.setClickDataBean(mGiftBeanList.get(position));
                mProjectOrGiftItemDialog.show();
                return true;
            }
        });
        updateList();
    }

    @Override
    protected View onGetView(int position, int size, DataBean dataBean, View convertView) {
        GiftBean giftBean = (GiftBean) dataBean;
        TextView tvGiftName = (TextView) convertView.findViewById(R.id.tvGiftName);
        TextView tvGiftScore = (TextView) convertView.findViewById(R.id.tvGiftScore);
        tvGiftName.setText(giftBean.getGiftName());
        tvGiftScore.setText(String.valueOf(0 - giftBean.getGiftScore()));
        return convertView;
    }

    @Override
    protected int getListItemLayout() {
        return R.layout.gift_list_item;
    }

    @Override
    public void updateList() {
        mGiftBeanList = getShowDataList(DBManager.getInstance().queryGitBeanList());
        notifyListDataChanged(mGiftBeanList);
    }

    @Override
    protected View getEmptyView(View view) {
        return view.findViewById(R.id.emptyView);
    }

    @Override
    protected void onScrollDirectionChanged(boolean isScrollUp) {
        IListFragmentChanged.onListScrollDirectionChanged(isScrollUp);
    }

    private List<DataBean> getShowDataList(List<GiftBean> originList) {
        List<DataBean> showList = new ArrayList<>();
        for (GiftBean giftBean : originList) {
            if (!giftBean.isCompleted()) {
                showList.add(giftBean);
            }
        }
        return showList;
    }

    class DialogDialogItemClickOr implements IProjectOrGiftDialogItemClick {

        @Override
        public void onEditClick(DataBean dataBean) {
            IListFragmentChanged.onCreateViewEdit(MainActivity.FragmentType.GiftFragment, dataBean);
        }

        @Override
        public void onDeleteClick(DataBean dataBean) {
            DBManager.getInstance().deleteGiftData((GiftBean) dataBean);
            updateList();
        }
    }

    private void showGiftDialog(final GiftBean giftBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.tip);
        builder.setMessage(String.format(getString(R.string.gitScoreTip), giftBean
                .getGiftScore()));
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int score = DBManager.getInstance().getScore();
                if (score < giftBean.getGiftScore()) {
                    Toast.makeText(GiftFragment.this.getActivity(), R.string.hasPoorScore, Toast
                            .LENGTH_SHORT).show();
                    return;
                }
                updateGiftData(giftBean);
                updateScore(giftBean);
                updateList();
            }
        });
        builder.create().show();
    }

    private void updateScore(GiftBean giftBean) {
        int score = DBManager.getInstance().getScore() - giftBean.getGiftScore();
        DBManager.getInstance().updateUserScore(score);
    }

    private void updateGiftData(GiftBean giftBean) {
        if (giftBean.getGiftType() == GiftBean.GiftType.Single) {
            giftBean.setCompleted(true);
            DBManager.getInstance().updateGiftData(giftBean);
        }

        String completeDate = String.valueOf(DateUtils.dayBegin(new Date()).getTime());
        String completeSpecificTime = String.valueOf(new Date().getTime());

        giftBean.setGiftCompleteDate(completeDate);
        giftBean.setGiftCompleteSpecificTime(completeSpecificTime);

        DBManager.getInstance().insertHistoryGiftData(giftBean);
    }

    public void setIListFragmentChanged(com.achilles.callback.IListFragmentChanged
                                                IListFragmentChanged) {
        this.IListFragmentChanged = IListFragmentChanged;
    }
}
